package com.sherlockHolmes.init.base.filter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class CorsFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        // 设置允许跨域访问的域名，可以使用通配符 *
        httpResponse.setHeader("Access-Control-Allow-Origin", "*");

        // 设置允许的请求方法
        httpResponse.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");

        // 设置允许的请求头部信息
        httpResponse.setHeader("Access-Control-Allow-Headers", "Content-Type");

        // 设置允许携带认证信息(Cookies)
        httpResponse.setHeader("Access-Control-Allow-Credentials", "true");

        chain.doFilter(request, response);
    }
}
